package com.edu.chongdian;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.edu.chongdian.View.LoginActivity;

import cn.leancloud.LeanCloud;

public class SplashActivity extends AppCompatActivity {
    //启动页
    private static final int TIME_DELAY = 3000; // 延迟时间
    private Handler handler = new Handler();
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        sp = getSharedPreferences("login_info", MODE_PRIVATE);
        LeanCloud.initialize(this, "LUJ9MABSnxyG2ClRKN6iVsqJ-gzGzoHsz", "9DmshcvJ40rofCd3zfR0dVZZ", "https://luj9mabs.lc-cn-n1-shared.com");
        // 延迟三秒
        handler.postDelayed(() -> {
            // 检查本地存储的账号和密码是否存在
            checkLocalLoginInfo();
        }, TIME_DELAY);

    }
    private void checkLocalLoginInfo() {
        // 获取本地存储的账号和密码
        String account = sp.getString("account", "");
        String password = sp.getString("password", "");

        // 检查本地存储的账号和密码是否存在
        boolean isExist = !TextUtils.isEmpty(account) && !TextUtils.isEmpty(password);

        // 如果本地存储的账号和密码都存在，则自动登录否则跳转到登录界面
        if (isExist) {
            autoLogin(account, password);
        }else{
            jumpToLogin();
        }
    }

    private void autoLogin(String account, String password) {
        // 在这里进行账号密码验证操作
        if (isValid(account, password)) {
            // 账号密码正确，跳转到主界面
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            // 账号密码错误，跳转到登录界面
            jumpToLogin();
        }
    }

    private void jumpToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private boolean isValid(String account, String password) {
        // 在这里进行账号密码验证操作
        boolean a;
        if (account.equals("admin") && password.equals("123456")){
            a=true;
        }else {
            a=false;
        }
        return a;
    }
}